<template>
    <div class="app-container">
        <el-card class="box-card">
            <div class="mb-4">
                <el-button type="primary" size="default" @click="handleAdd">新增</el-button>
            </div>
            <el-table
                ref="multipleTable"
                v-loading="loading"
                :data="tableData"
                border
                class="mt-3"
                stripe
                style="width: 100%"
                tooltip-effect="dark"
            >
                <el-table-column label="序号" type="index" :index="indexMethod" prop="user_code" width="100px"></el-table-column>
                <el-table-column label="创建时间" prop="created_at" width="400px"></el-table-column>
                <el-table-column label="标签内容" prop="title"></el-table-column>
                <el-table-column label="排序" prop="sort"></el-table-column>
                <el-table-column label="操作" width="200px">
                    <template slot-scope="scope">
                        <!-- <el-button
                            size="mini"
                            type="text"
                            @click="handleCheck(scope.row)"
                        >查看
                        </el-button> -->
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleSDelete(scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
    
            <!-- 分页 -->
            <cus-pagination
                v-show="total > 0"
                :limit.sync="listQuery.perPage"
                :page.sync="listQuery.page"
                :total="total"
                @pagination="getTableData"
            />
        </el-card>
  
        <el-dialog
            :visible.sync="dialogVisible"
            :title="dialogTitle"
            width="30%"
            @close="closeDialog('form')"
        >
            <el-form ref="form" :inline="false" :model="formData" :rules="rules" label-width="120px" size="normal">
                <el-form-item label="标签名称" prop="title">
                    <el-input v-model="formData.title" placeholder="标签名称" />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="formData.sort" placeholder="排序" />
                </el-form-item>
            </el-form>
    
            <span slot="footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleSubmit">确定</el-button>
            </span>
        </el-dialog>
    
    </div>
</template>
  
<script>
    import { labelList, wishLabelSave, wishLabelDel } from '@/api/wishingHouse'
  
  	export default {
        name: 'refuseList',
        components: {},
        data() {
            return {
                dialogVisible: false,
                formData: {
                    title: undefined
                },
                rules: {
                    title: [{ required: true, message: '请输入标签名称', trigger: 'blur' }]
                },
                loading: false,
                tableData: [],
                listQuery: {
                    page: 1,
                    perPage: 10
                },
                total: 1,
                dialogTitle: undefined
            }
        },
        created() {
            this.getTableData()
        },
        methods: {
            indexMethod(index) {
                return this.total - (this.listQuery.page-1) * this.listQuery.perPage - index
            },
            // 网络请求
            /**
             * getTableData
             * @desc 获取表格数据
             */
            async getTableData() {
                this.loading = true
                for (let key in this.listQuery) {
                    this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key]
                }
                const {data, code} = await labelList(this.listQuery)
                if (code === 200) {
                    this.tableData = data.list
                    this.total = data.meta.total
                }
                this.loading = false
        
            },
            closeDialog(form) {
                if (this.$refs[form]) {
                    // this.formData.status = undefined
                    this.$refs[form].clearValidate() // 清除整个表单的校验
                    this.$refs[form].resetFields() // 清除整个表单的校验
                }
                this.formData = {
                    sort: undefined,
                    title: undefined
                }
            },
            handleCheck(row) {
                this.dialogVisible = true
                this.formData = row
            },
            handleAdd() {
                this.dialogVisible = true
                this.dialogTitle = '新增'
            },
            handleSubmit() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        
                        for (let key in this.formData) {
                            this.formData[key] = this.formData[key] === '' || this.formData[key] === undefined ? 0 : this.formData[key]
                        }
                        wishLabelSave(this.formData).then(res => {
                            if(res.code === 200) {
                                this.$message({
                                    type: 'success',
                                    message: '操作成功'
                                })
                            }
                            this.dialogVisible = false
                            this.getTableData()
                        })
                        
                    }
                })
            },
            handleSDelete(row) {
                this.$confirm('确定删除该标签？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    wishLabelDel({
                        id: row.id
                    }).then(res => {
                        if(res.code === 200) {
                            this.$message({
                                type: 'success',
                                message: '操作成功'
                            })
                            this.getTableData()
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
        }
    }
</script>
  
<style lang="scss" scoped>
    .infoBox {
        margin-top: 15px;
        margin-bottom: 15px;
        padding-bottom: 15px;
        border: 1px solid #999999;
        border-radius: 10px;
        display: flex;
        align-items: baseline;
        justify-content: center;
        box-sizing: border-box;
        padding-right: 30px;
        .info_1{
            display: flex;
            justify-content: center;
            flex-direction: column;
            width: 100%;

            .info_2{
            margin-top: 15px;
            display: flex;
            align-items: center;
            position: relative;

            span{
                width: 180px;
                text-align: right;
            }
            .tip{
                    color: red;
                    font-size: 12px;
                    position: absolute;
                    top: 100%;
                    left: 120px;
                }
            }
        }
    }
    .formItemBox{
        display: flex;
        align-items: center;
        flex-wrap: wrap;

        width: 100%;
        overflow: hidden;
    }
    ::v-deep {
        .formItemBox{
            .el-form-item {
                width: 50% !important;
            }
        }
        
    }

</style>
  